package com.gui.components;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;

import com.QuranParser;
import com.dataModel.Quran;
import com.dataModel.descriptors.AyahDescriptor;
import com.dataModel.descriptors.PageDescriptor;
import com.dataModel.page.DisplayAyah;
import com.dataModel.page.PageContent;
import com.dataModel.page.PageElement;
import com.dataModel.page.SourahHeader;
import com.panels.MainPanel;
import com.utils.gui.ArabicCharUtil;
import com.utils.gui.CharConnection;

public class QuranPage extends JTextPane {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2388178334117379199L;

	public QuranPage() {
		
		this.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		// this.setLineWrap(false);
		Font pageFont = new Font(Font.SANS_SERIF, Font.CENTER_BASELINE, 26);
		this.setFont(pageFont);
		Color pageColor = new Color(138, 198, 186);
		this.setBackground(pageColor);
		this.setEditable(false);
	}

	public void displayPage(int pageNumber) {

		PageContent pageContent = getPage(pageNumber);
		formatPage(pageContent);

	}

	public void formatPage(PageContent page) {
this.setText("");
		for (PageElement pageElement : page.getPageElements()) {
			if (pageElement instanceof DisplayAyah) {
				DisplayAyah displayAyah=(DisplayAyah)pageElement;
				try {
					this.getDocument().insertString(this.getDocument().getLength(), displayAyah.getTextMoshakal(), null);
			
					//JButton button=new RoundButton((displayAyah.getAyahID()+1)+"");
					JButton button=new JButton((displayAyah.getAyahID()+1)+"");
					button.setBackground(new Color(138, 198, 126));
					//button.setSize(button.getWidth(), button.getHeight()-20);
					button.setPreferredSize(new Dimension(11, 15));
					this.insertComponent(button);
				} catch (BadLocationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				

			} else if (pageElement instanceof SourahHeader) {
				try {
					this.getDocument().insertString(this.getDocument().getLength(),"\n", null);
					SourahHeader sourahHeader=(SourahHeader)pageElement;
					JButton button=new JButton(sourahHeader.toString());
					Dimension size=new Dimension(400,30);
					button.setMinimumSize(size);
					button.setPreferredSize(size);
				
				//	button.setFont(new Font)
					JPanel panel=new JPanel();
					panel.setLayout(new BorderLayout());
					panel.add(button,BorderLayout.CENTER);
					this.insertComponent(panel);
					this.getDocument().insertString(this.getDocument().getLength(),"\n", null);
				} catch (BadLocationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			

			}
		}

	}

	public String adjustLine(String line, int desiredSize) {
		String adjustedLine = line.trim();
		String[] words = line.trim().split(" ");
		int count = 0;

		int currentSize = this.getGraphics().getFontMetrics().charsWidth(
				adjustedLine.toCharArray(), 0, adjustedLine.length());
		int waslahSize = this.getGraphics().getFontMetrics().stringWidth(
				"\u0640");

		String waslah = "";
		for (int i = 0; i < waslahSize; i++) {
			waslah += "\u0640";
		}

		double maxSize = ((float) (desiredSize - currentSize) / (float) waslahSize);
		int waslahNb = (maxSize - (int) maxSize == 0) ? (int) maxSize
				: ((int) maxSize) + 1;
		int insertionPoint = -1;
		for (int i = adjustedLine.length() - 1; i >= 1; i--) {
			char b = adjustedLine.charAt(i);
			char a = adjustedLine.charAt(i - 1);
			if ((CharConnection.BOTH
					.equals(ArabicCharUtil.getCharConnection(b)) || CharConnection.RIGHT
					.equals(ArabicCharUtil.getCharConnection(b)))
					&& (CharConnection.BOTH.equals(ArabicCharUtil
							.getCharConnection(a)) || CharConnection.LEFT
							.equals(ArabicCharUtil.getCharConnection(a)))) {
				System.out.println(ArabicCharUtil.getCharConnection(a));
				insertionPoint = i;
				break;
			}
		}
		if (insertionPoint != -1)
			while (this.getGraphics().getFontMetrics()
					.stringWidth(adjustedLine) < desiredSize) {
				// adjustedLine += "\u0640";
				adjustedLine = new StringBuilder(adjustedLine).insert(
						insertionPoint, "\u0640").toString();
				count++;
			}

		return adjustedLine;
	}

	public PageContent getPage(int pageNumber) {

		if (pageNumber < 1 || pageNumber > Quran.TAHA_OSMAN_PAGE_NUMBERS) {
			return null;
		}

		PageContent pageContent = new PageContent();
		String pageString = "";
		PageDescriptor pageDescriptor = Quran.getQuranPages()[pageNumber];
		AyahDescriptor startAyah = pageDescriptor.getStartAyah();
		AyahDescriptor endAyah = (pageNumber + 1 != Quran.TAHA_OSMAN_PAGE_NUMBERS) ? Quran
				.getQuranPages()[pageNumber + 1].getStartAyah()
				: new AyahDescriptor(115, 0);

		for (int i = startAyah.getSourahNumber(); i <= endAyah
				.getSourahNumber(); i++) {
			int lastAyah = 0;
			int firstAyah = 0;

			if (i == startAyah.getSourahNumber()
					&& endAyah.getSourahNumber() == i) {
				firstAyah = startAyah.getAyahNumber() - 1;
				lastAyah = endAyah.getAyahNumber() - 1;
			} else if (i == startAyah.getSourahNumber()
					&& endAyah.getSourahNumber() >= i) {
				firstAyah = startAyah.getAyahNumber() - 1;
				lastAyah = QuranParser.getQuran().getSowar()[startAyah
						.getSourahNumber() - 1].getAyat().size();
			} else if (i > startAyah.getSourahNumber()) {
				firstAyah = 0;
				if (i < endAyah.getSourahNumber()) {
					lastAyah = QuranParser.getQuran().getSowar()[i - 1]
							.getAyat().size();
				} else {
					lastAyah = endAyah.getAyahNumber() - 1;
				}
			}

			for (int j = firstAyah; j < lastAyah; j++) {
				if (j == 0) {

					pageContent.getPageElements().add(new SourahHeader(i - 1));

				}
				pageContent.getPageElements().add(new DisplayAyah(j, i - 1));

			}

		}
		return pageContent;
	}

}
